package com.innerchic.weqiuqiu.ui;

import android.content.SharedPreferences;
import android.text.Editable;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;

import com.innerchic.annotations.base.ActivitySetting;
import com.innerchic.annotations.base.BindActivityLayoutHolder;
import com.innerchic.mapper.app.SuperActivity;
import com.innerchic.weqiuqiu.R;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：wedudu
 * 日    期：2022/1/13 16:08
 * 包    名：com.innerchic.weqiuqiu.ui
 * 描    述：功能设置
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */

@BindActivityLayoutHolder
@ActivitySetting(layout = "activity_function", modificationActionBar = false, textTheme = true, swipe = true)
public class FunctionSettingActivity extends SuperActivity implements CompoundButton.OnCheckedChangeListener {

    private FunctionSettingActivityLayoutHolder holder;

    @Override
    public void create() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            getSupportActionBar().setTitle(Html.fromHtml("<font color='#000'>功能设置</font>"));
        }

        //检查开启情况
        SharedPreferences function_status = getSharedPreferences("function_status", 0);
        boolean wechat_status = function_status.getBoolean("wechat_status", true);
        holder.wechatSwitch.setChecked(wechat_status);
        boolean qq_status = function_status.getBoolean("qq_status", false);
        holder.qqSwitch.setChecked(qq_status);
        boolean dingding_status = function_status.getBoolean("dingding_status", false);
        holder.dingdingSwitch.setChecked(dingding_status);
        boolean other_status = function_status.getBoolean("other_status", false);
        holder.otherSwitch.setChecked(other_status);
        holder.applicationSetting.setVisibility(holder.otherSwitch.isChecked() ? View.VISIBLE : View.GONE);

        //检查各二级菜单开启情况
        SharedPreferences function_wechat_status = getSharedPreferences("function_wechat_status", 0);
        boolean wechat_reply_close_paopao = function_wechat_status.getBoolean("wechat_reply_close_paopao", true);//默认开启
        holder.oaceSwitch.setChecked(wechat_reply_close_paopao);

        //其他设置
        SharedPreferences function_other_status = getSharedPreferences("function_other_status", 0);
        //设置是否开启定时清理
        boolean regular_cleaning_paopao = function_other_status.getBoolean("regular_cleaning_paopao", true);//默认开启
        holder.timingSwitch.setChecked(regular_cleaning_paopao);
        //设置其他通知的销毁时间,如果开启定时清理的话
        int regular_cleaning_paopao_timer = function_other_status.getInt("regular_cleaning_paopao_timer", 2);
        holder.timingEditText.setText(String.valueOf(regular_cleaning_paopao_timer));
        holder.timingEditText.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                holder.setTimingBtn.setVisibility(View.VISIBLE);
            } else {
                holder.setTimingBtn.setVisibility(View.GONE);
            }
        });
        holder.setTimingBtn.setOnClickListener(v -> {
            Editable text = holder.timingEditText.getText();
            if (text == null) {
                showToast("输入时间不可以为null");
                return;
            }
            int defTime = 2;
            try {
                defTime = Integer.parseInt(text.toString());
            } catch (Exception e) {
                showToast("不合法的参数");
                return;
            }
            function_other_status.edit().putInt("regular_cleaning_paopao_timer", defTime).apply();
            holder.timingEditText.clearFocus();//失去焦点
        });

        //设置监听
        holder.wechatSwitch.setOnCheckedChangeListener(this);
        holder.wechatSetting.setOnClickListener(v -> {
            holder.wechatSwitch.setChecked(!holder.wechatSwitch.isChecked());
        });
        holder.qqSwitch.setOnCheckedChangeListener(this);
        holder.qqSetting.setOnClickListener(v -> {
            holder.qqSwitch.setChecked(!holder.qqSwitch.isChecked());
        });
        holder.dingdingSwitch.setOnCheckedChangeListener(this);
        holder.dingdingSetting.setOnClickListener(v -> {
            holder.dingdingSwitch.setChecked(!holder.dingdingSwitch.isChecked());
        });
        holder.otherSwitch.setOnCheckedChangeListener(this);
        holder.otherSetting.setOnClickListener(v -> {
            holder.otherSwitch.setChecked(!holder.otherSwitch.isChecked());
        });
        //二级菜单
        holder.oaceSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            //回复了消息之后是否移除泡泡
            getSharedPreferences("function_wechat_status", 0).edit().putBoolean("wechat_reply_close_paopao", isChecked).apply();
        });

        holder.timingSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            //其他通知是否开启定时清理
            getSharedPreferences("function_other_status", 0).edit().putBoolean("regular_cleaning_paopao", isChecked).apply();
            holder.timingTimeSetting.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        });
    }

    @Override
    public void destroy() {
        //如果没点在这里存储
        if (holder.timingEditText != null) {
            Editable text = holder.timingEditText.getText();
            if (text != null) {
                try {
                    int times = Integer.parseInt(text.toString());
                    getSharedPreferences("function_other_status", 0).edit().putInt("regular_cleaning_paopao_timer", times).apply();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.close_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.action_close) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String key = null;
        if (buttonView.getId() == R.id.wechatSwitch) {
            key = "wechat_status";
        } else if (buttonView.getId() == R.id.qqSwitch) {
            key = "qq_status";
        } else if (buttonView.getId() == R.id.dingdingSwitch) {
            key = "dingding_status";
        } else if (buttonView.getId() == R.id.otherSwitch) {
            key = "other_status";
            showOtherMethod(isChecked);
        } else {
            return;
        }
        getSharedPreferences("function_status", 0).edit().putBoolean(key, isChecked).apply();
    }

    /**
     * 显示其他通知的二级菜单的方法
     */
    private void showOtherMethod(boolean isChecked) {
        holder.applicationSetting.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        if (isChecked) {
            holder.timingOpenSetting.setVisibility(View.VISIBLE);
            if (getSharedPreferences("function_other_status", 0).getBoolean("regular_cleaning_paopao", true)) {
                holder.timingTimeSetting.setVisibility(View.VISIBLE);
            } else {
                holder.timingTimeSetting.setVisibility(View.GONE);
            }
        } else {
            holder.timingOpenSetting.setVisibility(View.GONE);
            holder.timingTimeSetting.setVisibility(View.GONE);
        }
    }
}
